////////////////////////////////////////////////
// -- Inscript                                //
// by Hurricaaane (Ha3)                       //
//                                            //
// http://www.youtube.com/user/Hurricaaane    //
//--------------------------------------------//
// Menu                                       //
////////////////////////////////////////////////

include( 'CtrlColorExtended.lua' )
include( 'InsStyleCheckBox.lua' )
include( 'InsMount.lua' )
include( 'InsTools.lua' )

local INSCRIPT_LOCAL_MENU_WIDTH = 300

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// DERMA PANEL .


function inscript.Util_FrameGetExpandTable( myPanel )
	local expandTable = {}
	
	for k,subtable in pairs( myPanel.Categories ) do
		table.insert(expandTable, subtable[1]:GetExpanded())
		
	end
	
	return expandTable
end

function inscript.Util_AppendPanel( myPanel, thisPanel )
	local toAppendIn = myPanel.Categories[#myPanel.Categories][1].List
	
	thisPanel:SetParent( toAppendIn )
	toAppendIn:AddItem( thisPanel )
	
end

function inscript.Util_AppendCheckBox( myPanel, title, cvar )

	local checkbox = vgui.Create( "DCheckBoxLabel" )
	checkbox:SetText( title )
	checkbox:SetConVar( cvar )
	
	inscript.Util_AppendPanel( myPanel, checkbox )
	
end

function inscript.Util_AppendLabel( myPanel, sText, optiSize, optbWrap )

	local label = vgui.Create( "DLabel" )
	label:SetText( sText )
	
	--local useSY = false
	
	if optiSize then
		label:SetWrap( true )
		label:SetContentAlignment( 2 )
		if (type(optiSize) == "number") then
			label:SetSize( myPanel.W_WIDTH, optiSize )
		
		else
			--useSY = true
			label:SetWide( myPanel.W_WIDTH )
			label:SetAutoStretchVertical( true )
			--label:PerformLayout( )
			--label:SizeToContentsY( )
			--label:SizeToContents()
			
		end
		
	end
	
	if optbWrap then
		label:SetWrap( true )
		
	end
	
	inscript.Util_AppendPanel( myPanel, label )
	--if useSY then label:PerformLayout() label:SizeToContents() end
	
end

function inscript.Util_AppendSlider( myPanel, sText, sCvar, fMin, fMax, iDecimals)
	local slider = vgui.Create("DNumSlider")
	slider:SetText( sText )
	slider:SetMin( fMin )
	slider:SetMax( fMax )
	slider:SetDecimals( iDecimals )
	slider:SetConVar( sCvar )
	
	inscript.Util_AppendPanel( myPanel, slider )
end

function inscript.Util_MakeFrame( width, height, optsTitle )
	local myPanel = vgui.Create( "DFrame" )
	local border = 4
	
	myPanel.W_HEIGHT = height - 20
	myPanel.W_WIDTH = width - 2 * border
	
	myPanel:SetPos( ScrW() * 0.5 - width * 0.5 , ScrH() * 0.5 - height * 0.5 )
	myPanel:SetSize( width, height )
	myPanel:SetTitle( INSCRIPT_NAME )
	myPanel:SetVisible( false )
	myPanel:SetDraggable( true )
	myPanel:ShowCloseButton( true )
	myPanel:SetDeleteOnClose( false )
	
	myPanel.Contents = vgui.Create( "DPanelList", myPanel )
	myPanel.Contents:SetPos( border , 22 + border )
	myPanel.Contents:SetSize( myPanel.W_WIDTH, height - 2 * border - 22 )
	myPanel.Contents:SetSpacing( 5 )
	myPanel.Contents:EnableHorizontal( false )
	myPanel.Contents:EnableVerticalScrollbar( false )
	
	myPanel.Categories = {}
	
	return myPanel
end

function inscript.Util_MakeCategory( myPanel, sTitle, bExpandDefault )
	local category = vgui.Create("DCollapsibleCategory", myPanel.Contents)
	category.List  = vgui.Create("DPanelList", category )
	table.insert( myPanel.Categories, {category, bExpandDefault} )
	category:SetSize( myPanel.W_WIDTH, 50 )
	category:SetLabel( sTitle )
	
	category.List:SetPadding( 2 )
	category.List:SetSpacing( 2 )
	
	category.List:EnableHorizontal( false )
	category.List:EnableVerticalScrollbar( false )
	
	return category
end

function inscript.Util_ApplyCategories( myPanel, opt_tExpand )
	for k,subtable in pairs( myPanel.Categories ) do
		subtable[1]:SetExpanded( opt_tExpand and (opt_tExpand[k] and 1 or 0) or subtable[2] )
		subtable[1].List:SetSize( myPanel.W_WIDTH, myPanel.W_HEIGHT - #myPanel.Categories * 10 - 10 )
		subtable[1]:SetSize( myPanel.W_WIDTH, myPanel.W_HEIGHT - #myPanel.Categories * 10 )
		
		subtable[1].List:PerformLayout()
		subtable[1].List:SizeToContents()
		
		subtable[1]:SetContents( subtable[1].List )
		
		myPanel.Contents:AddItem( subtable[1] )
	end
	
end

function inscript.MenuCall_MountChecked()
	local refPanel = inscript.DermaPanel

	inscript.MountDesiredPlugins()
	
	if refPanel and refPanel.mountbuttons then
		for k,button in pairs(refPanel.mountbuttons) do
			button:UpdateLabel()
		end
	end
end

function inscript.MenuCall_Reload()
	local refPanel = inscript.DermaPanel
	
	local myExpand = inscript.Util_FrameGetExpandTable( refPanel )
	local myX, myY = refPanel:GetPos()
	inscript.LoadAllPlugins()
	inscript.BuildMenu( myExpand, myX, myY )
	inscript.ShowMenu()
end

function inscript.BuildMenu( opt_tExpand, opt2_iXpos, opt2_iYpos )
	if inscript.DermaPanel then inscript.DermaPanel:Remove() end
	
	local MY_VERSION, SVN_VERSION, DOWNLOAD_LINK = inscript.GetVersionData()
	
	inscript.DermaPanel = inscript.Util_MakeFrame( INSCRIPT_LOCAL_MENU_WIDTH, ScrH() * 0.7 )
	local refPanel = inscript.DermaPanel
	
	inscript.Util_MakeCategory( refPanel, "General", (MY_VERSION and SVN_VERSION and (MY_VERSION < SVN_VERSION)) and 1 or 0 )
	inscript.Util_AppendCheckBox( refPanel, "Auto-mount checked scripts on Start" , "inscript_core_enable" )
	
	inscript.Util_AppendLabel( refPanel, "Note: Checking or unckecking a script does not automatically mount it.", 40, true )
	
	--Mount button
	do
		local CMountAll = vgui.Create("DButton")
		CMountAll:SetText( "Mount checked scripts" )
		CMountAll.DoClick = inscript.MenuCall_MountChecked
		
		inscript.Util_AppendPanel( refPanel, CMountAll )
	end
	
	-- Reload Button
	do
		local CReloadList = vgui.Create("DButton")
		CReloadList:SetText( "Reload scripts" )
		CReloadList.DoClick = inscript.MenuCall_Reload
		inscript.Util_AppendPanel( refPanel, CReloadList )
	end
	
	--Helper label
	do
		local GeneralTextLabelMessage = "The command \"inscript_menu\" calls this menu.\n"
		if not (MY_VERSION and SVN_VERSION and (MY_VERSION < SVN_VERSION)) then
			GeneralTextLabelMessage = GeneralTextLabelMessage .. "Example : To assign " .. INSCRIPT_NAME .. " menu to F9, type in the console :"
			
		else
			GeneralTextLabelMessage = GeneralTextLabelMessage .. "Your version is "..MY_VERSION.." and the updated one is "..SVN_VERSION.." ! You should update !"
			
		end
		inscript.Util_AppendLabel( refPanel, GeneralTextLabelMessage, 40, true )
		
	end
	--Helper multiline
	do
		local GeneralCommandLabel = vgui.Create("DTextEntry")
		local hasUpdate = (MY_VERSION and SVN_VERSION and (MY_VERSION < SVN_VERSION) and DOWNLOAD_LINK)
		
		if not hasUpdate then
			GeneralCommandLabel:SetText( "bind \"F9\" \"inscript_menu\"" )
			
		else
			GeneralCommandLabel:SetText( DOWNLOAD_LINK )
			
		end
		GeneralCommandLabel:SetEditable( false )
		if hasUpdate then
			GeneralCommandLabel:SetMultiline( true )
			GeneralCommandLabel:SetSize( refPanel.W_WIDTH, 60 )
		end

		inscript.Util_AppendPanel( refPanel, GeneralCommandLabel )
		
	end
	inscript.Util_AppendLabel( refPanel, "You can quickly call the menu by binding \"+inscript_menu\" to one of your keys : The menu closes when you release the key.", 40, true )
	
	inscript.Util_MakeCategory( refPanel, "Inscripts", 1 )
	inscript.Util_AppendPanel( refPanel, vgui.Create("InsTools") )
	refPanel.mountbuttons = {}
	for k,sName in pairs( inscript_plugin.GetNamesTable() ) do		
		local CMountBasket = vgui.Create("InsMount")
		CMountBasket:SetMasterName( sName )
		table.insert( refPanel.mountbuttons , CMountBasket )
		
		inscript.Util_AppendPanel( refPanel, CMountBasket )
	end
	
	inscript.Util_ApplyCategories( refPanel, opt_tExpand )
	
	if opt2_iXpos and opt2_iYpos then
		refPanel:SetPos( opt2_iXpos, opt2_iYpos )
	end
end

function inscript.ShowMenu()
	if not inscript.DermaPanel then
		inscript.BuildMenu()
	end
	--inscript.DermaPanel:Center()
	inscript.DermaPanel:MakePopup()
	inscript.DermaPanel:SetKeyboardInputEnabled( false )
	inscript.DermaPanel:SetVisible( true )
end

function inscript.HideMenu()
	if not inscript.DermaPanel then
		return
	end
	inscript.DermaPanel:SetVisible( false )
end

function inscript.DestroyMenu()
	if inscript.DermaPanel then
		inscript.DermaPanel:Remove()
		inscript.DermaPanel = nil
	end
end

--[[
function inscript.MakePresetPanel( data )
	local ctrl = vgui.Create( "ControlPresets", self )
	
	ctrl:SetPreset( data.folder )
	
	if ( data.options ) then
		for k, v in pairs( data.options ) do
			if ( k != "id" ) then
				ctrl:AddOption( k, v )
			end
		end
	end
	
	if ( data.cvars ) then
		for k, v in pairs( data.cvars ) do
			ctrl:AddConVar( v )
		end
	end
	
	return ctrl
end
]]--

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//// SANDBOX PANEL .

function inscript.Panel(Panel)	
	Panel:AddControl("Checkbox", {
			Label = "Enable Auto-mount", 
			Description = "Enable Auto-mount", 
			Command = "inscript_core_enable" 
		}
	)
	Panel:AddControl("Button", {
			Label = "Open Menu (inscript_menu)", 
			Description = "Open Menu (inscript_menu)", 
			Command = "inscript_menu"
		}
	)
	
	Panel:Help("To trigger the menu in any gamemode, type inscript_menu in the console, or bind this command to any key.")
end

function inscript.AddPanel()
	spawnmenu.AddToolMenuOption("Options", "Player", INSCRIPT_NAME, INSCRIPT_NAME, "", "", inscript.Panel, {SwitchConVar = 'inscript_core_enable'})
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// MOUNT FCTS.

function inscript.MountMenu()
	concommand.Add( "inscript_menu", inscript.ShowMenu )
	concommand.Add( "inscript_call_menu", inscript.ShowMenu )
	concommand.Add( "+inscript_menu", inscript.ShowMenu )
	concommand.Add( "-inscript_menu", inscript.HideMenu )
	hook.Add( "PopulateToolMenu", "AddInscriptPanel", inscript.AddPanel )
end

function inscript.UnmountMenu()
	inscript.DestroyMenu()

	concommand.Remove( "inscript_call_menu" )
	concommand.Remove( "inscript_menu" )
	concommand.Remove( "+inscript_menu" )
	concommand.Remove( "-inscript_menu" )
	hook.Remove( "PopulateToolMenu", "AddInscriptPanel" )
end

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////